const {app, BrowserWindow, Menu, Tray, ipcMain} = require('electron')
const isDev = require('electron-is-dev');
const path = require('path')

let win, tray

app.on('ready', () => {
    ipcInit();
    trayInit();
    winInit();
    openURL();
})

function trayInit(){
    tray = new Tray(path.join(__dirname, "icon/logo.png"));
    const contextMenu = Menu.buildFromTemplate([
        {
            label: "打开", click: () => {
                win.show()
                win.webContents.openDevTools()
            }
        },
        {
            label: "退出", click: () => {
                win.webContents.send("close")
            }
        }
    ])
    tray.on("double-click", () => {
        win.show()
    })
    tray.setToolTip('凤梨网盘')
    tray.setContextMenu(contextMenu)
}

function winInit(){
    Menu.setApplicationMenu(null)
    win = new BrowserWindow({
        width: 1280,
        height: 800,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true
        }
    })

    win.on("close", (e) => {
        e.preventDefault()
        win.hide()
    })
}

function openURL(){
    if (isDev) {
        win.loadURL('http://localhost:8080')
        win.webContents.openDevTools();

    } else {
        win.loadFile(path.resolve(__dirname, '../renderer/pages/main/index.html'))

    }
}

function ipcInit(){
    ipcMain.on("close",()=>{
        app.exit(0)
    })
}
